package com.project.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.project.bean.GoodsBean;
import com.project.mapper.IGoodsMapper;
import com.project.service.IGoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class GoodsServiceImpl implements IGoodsService {
    @Autowired
    private IGoodsMapper mapper;
    @Override
    public IPage<GoodsBean> findByItem(Integer pageNo, String name, Integer startNum, Integer endNum, Integer price, String genre) {
        QueryWrapper<GoodsBean> qw = new QueryWrapper<>();
        if (name!=null&&name.length()!=0){
            qw.like("g_name",name);
        }
        if (startNum!=null){
            qw.ge("g_num",startNum);
        }
        if (endNum!=null){
            qw.le("g_num",endNum);
        }
        if (price==1){
            qw.ge("g_price",0);
        }
        if (price==2){
            qw.eq("g_price",null);
            qw.eq("g_price",0);
        }
        if (genre!=null&&genre.length()!=0){
            qw.eq("g_genre",genre);
        }
        return mapper.selectPage(new Page(pageNo,3),qw);
    }

    @Override
    public GoodsBean findById(Integer number) {
        QueryWrapper<GoodsBean> qw = new QueryWrapper<>();
         qw.eq("g_number",number);
        return mapper.selectOne(qw);
    }

    @Override
    public void update(Integer id, Integer sellPrice) {
       mapper.update(id,sellPrice);
    }

    @Override
    public List<GoodsBean> findAll() {
        return mapper.selectList(null);
    }
}
